package com.jvxb.manage.livable.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Condition;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jvxb.common.utils.CommonUtil;
import com.jvxb.common.web.RespMsg;
import com.jvxb.manage.livable.entity.SysRole;
import com.jvxb.manage.livable.entity.SysRolePerm;
import com.jvxb.manage.livable.service.SysRolePermService;
import com.jvxb.manage.livable.service.SysRoleService;
import com.jvxb.manage.livable.vo.SysRoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 系统角色表 前端控制器
 * </p>
 *
 * @author jvxb
 * @since 2020-06-07
 */
@RestController
@RequestMapping("/admin/v1/sysRole")
@Api(tags = "角色相关接口")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysRolePermService sysRolePermService;

    @GetMapping("list")
    @ApiOperation("角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "角色名", required = false, dataType = "String"),
    })
    public RespMsg<List<SysRole>> list(String name) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like(StrUtil.isNotEmpty(name), SysRole.ROLE_NAME, name);
        return RespMsg.ok(sysRoleService.list(wrapper));
    }

    @GetMapping("listPage")
    @ApiOperation("角色分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "角色名", required = false, dataType = "String"),
            @ApiImplicitParam(name = "size", value = "每页条数，默认10条", dataType = "Integer", required = false),
            @ApiImplicitParam(name = "current", value = "第几页,默认第一页", dataType = "Integer", required = false)
    })
    public RespMsg<IPage<SysRole>> listPage(String name,
                                            @RequestParam(defaultValue = "10") Integer size,
                                            @RequestParam(defaultValue = "1") Integer current) {
        IPage<SysRole> page = new Page<>(current, size);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like(StrUtil.isNotEmpty(name), SysRole.ROLE_NAME, name);
        sysRoleService.page(page, wrapper);
        return RespMsg.ok(page);
    }

    @GetMapping("one")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色id", required = false, dataType = "Integer")
    })
    public RespMsg<SysRole> one(@RequestParam Integer id) {
        SysRole sysRole = sysRoleService.getById(id);
        return RespMsg.ok(sysRole);
    }

    /**
     * 保存角色
     */
    @ResponseBody
    @PostMapping(value = {"add", "update"})
    public RespMsg<SysRole> add(@RequestBody SysRoleVo sysRoleVo) {
        //新增或修改角色信息
        SysRole sysRole = new SysRole(sysRoleVo.getRoleId(), sysRoleVo.getRoleName(), sysRoleVo.getRoleDesc(), new Date());
        //如果是update，先删除原来的角色-权限关系，再建立新的
        if (CommonUtil.notNullOrEmpty(sysRole.getRoleId())) {
            sysRolePermService.remove(Condition.<SysRolePerm>create().eq(SysRolePerm.ROLE_ID, sysRoleVo.getRoleId()));
        }
        sysRoleService.saveOrUpdate(sysRole);
        //保存角色-权限关系
        String perms = sysRoleVo.getPerms();
        if (CommonUtil.notNullOrEmpty(perms)) {
            String[] permArr = perms.split(",");
            List<SysRolePerm> sysRolePermList = new ArrayList<>();
            Date now = new Date();
            Arrays.stream(permArr).forEach(e -> sysRolePermList.add(new SysRolePerm(sysRole.getRoleId(), Integer.valueOf(e), now)));
            sysRolePermService.saveOrUpdateBatch(sysRolePermList);
        }
        return RespMsg.ok(sysRole);
    }

    @PostMapping("delete")
    public RespMsg<SysRole> delete(@RequestBody SysRole sysRole) {
        sysRoleService.removeById(sysRole);
        sysRolePermService.remove(Condition.<SysRolePerm>create().eq(SysRolePerm.ROLE_ID, sysRole.getRoleId()));
        return RespMsg.ok();
    }

    @PostMapping("batchDelete")
    public RespMsg<SysRole> batchDelete(@RequestBody Map param) {
        List<Integer> ids = (List<Integer>) param.get("ids");
        for (Integer id : ids) {
            sysRoleService.removeById(id);
            sysRolePermService.remove(Condition.<SysRolePerm>create().eq(SysRolePerm.ROLE_ID, id));
        }
        return RespMsg.ok();
    }
}

